---@diagnostic disable: lowercase-global, undefined-global

-- 溺水系统的RPC处理器
-- 用于处理客户端黑屏效果和动画

local modid = 'mod_pono'

-- 【注册溺水黑屏RPC - 客户端接收】
AddClientModRPCHandler(modid, "drowning_screenfade", function(duration)
    print("[Drowning RPC Client] Received screenfade RPC, duration:", duration)
    if ThePlayer and ThePlayer.HUD then
        print("[Drowning RPC Client] Creating screen overlay")
        
        -- 禁用玩家控制（客户端）
        if ThePlayer.components.playercontroller then
            ThePlayer.components.playercontroller:Enable(false)
            print("[Drowning RPC Client] Player controller disabled")
        end
        
        -- 禁用移动
        if ThePlayer.components.locomotor then
            ThePlayer.components.locomotor:Stop()
            ThePlayer.components.locomotor:SetExternalSpeedMultiplier(ThePlayer, "drowning", 0)
            print("[Drowning RPC Client] Movement disabled")
        end
        
        -- 使用HUD的黑屏覆盖层（最安全的方法）
        local overlay = ThePlayer.HUD:AddChild(Image("images/global.xml", "square.tex"))
        if overlay then
            overlay:SetVRegPoint(ANCHOR_MIDDLE)
            overlay:SetHRegPoint(ANCHOR_MIDDLE)
            overlay:SetVAnchor(ANCHOR_MIDDLE)
            overlay:SetHAnchor(ANCHOR_MIDDLE)
            overlay:SetScaleMode(SCALEMODE_FILLSCREEN)
            overlay:SetTint(0, 0, 0, 1)  -- 黑色
            overlay:SetClickable(false)
            print("[Drowning RPC Client] Black overlay created")
            
            -- 保持黑屏指定时间后开始渐变淡出
            ThePlayer:DoTaskInTime(duration, function()
                if overlay and ThePlayer and ThePlayer:IsValid() then
                    print("[Drowning RPC Client] Starting fade out")
                    
                    -- 渐变淡出效果（1秒内从不透明到透明）
                    local fade_duration = 1.0  -- 淡出持续时间
                    local fade_steps = 20      -- 淡出步数（越多越平滑）
                    local step_time = fade_duration / fade_steps
                    local current_step = 0
                    
                    overlay.fade_task = overlay.inst:DoPeriodicTask(step_time, function()
                        current_step = current_step + 1
                        local alpha = 1 - (current_step / fade_steps)  -- 从1递减到0
                        
                        if overlay and overlay.SetTint then
                            overlay:SetTint(0, 0, 0, alpha)
                        end
                        
                        -- 淡出完成后移除覆盖层
                        if current_step >= fade_steps then
                            if overlay.fade_task then
                                overlay.fade_task:Cancel()
                                overlay.fade_task = nil
                            end
                            if overlay then
                                overlay:Kill()
                                print("[Drowning RPC Client] Overlay removed after fade out")
                            end
                        end
                    end)
                end
            end)
        else
            print("[Drowning RPC Client] ERROR: Could not create overlay")
        end
    else
        print("[Drowning RPC Client] ERROR: ThePlayer or HUD not found")
    end
end)

-- 【注册溺水wakeup动画RPC - 客户端接收】
AddClientModRPCHandler(modid, "drowning_wakeup", function()
    print("[Drowning RPC Client] Received wakeup RPC")
    if ThePlayer and ThePlayer.sg then
        if ThePlayer.sg.sg.states.wakeup then
            ThePlayer.sg:GoToState("wakeup")
            print("[Drowning RPC Client] Wakeup animation started")
        else
            ThePlayer.sg:GoToState("idle")
            print("[Drowning RPC Client] No wakeup state, going to idle")
        end

        -- wakeup动画后延迟1.8秒恢复控制（与服务器端同步）
        ThePlayer:DoTaskInTime(1.8, function()
            if ThePlayer and ThePlayer:IsValid() then
                -- 恢复玩家控制
                if ThePlayer.components.playercontroller then
                    ThePlayer.components.playercontroller:Enable(true)
                    print("[Drowning RPC Client] Player controller enabled")
                end
                
                -- 恢复移动
                if ThePlayer.components.locomotor then
                    ThePlayer.components.locomotor:RemoveExternalSpeedMultiplier(ThePlayer, "drowning")
                    print("[Drowning RPC Client] Movement enabled")
                end
            end
        end)
    else
        print("[Drowning RPC Client] ERROR: ThePlayer or sg not found")
    end
end)
